package com.one.rope.mvp.basecore.bean;

import com.one.rope.mvp.basecore.util.ExceptionUtils;
import com.one.rope.mvp.basecore.util.JsonUtils;

/**
 * @author Weijian.liu
 * @desc 接口返回错误信息
 * @date 2018/11/26
 */
public class ResponseMessage {

  /**
   * 提示信息
   */
  private String msg;

  /**
   * 错误码
   */
  private int code;

  public ResponseMessage(int code, String message) {
    this.code = code;
    this.msg = message;
  }

  public static ResponseMessage instance(int code, String message) {
    return new ResponseMessage(code, message);
  }

  public static ResponseMessage systemError(Throwable e) {
    String exString = ExceptionUtils.getStackTrace(e);
    return ResponseMessage.instance(BaseBusinessMessage.FAILURE_CODE, exString);
  }

  public static ResponseMessage userFailure(String message) {
    return ResponseMessage.instance(BaseBusinessMessage.USERFAILURE_CODE, message);
  }

  @Override
  public boolean equals(Object obj) {
    if (obj instanceof ResponseMessage) {
      ResponseMessage tMessage = (ResponseMessage) obj;
      return this.code == tMessage.code;
    }
    return false;
  }

  @Override
  public int hashCode() {
    return Integer.valueOf(this.code).hashCode();
  }

  public String getMsg() {
    return msg;
  }

  public void setMsg(String msg) {
    this.msg = msg;
  }

  public int getCode() {
    return code;
  }

  public void setCode(int code) {
    this.code = code;
  }

  @Override
  public String toString() {
    return JsonUtils.objectToFasterxmlJsonString(this);
  }
}
